//
//  XSHeaderView.m
//  ShopDemo
//
//  Created by Sunshine on 2018/8/13.
//  Copyright © 2018年 飞扬的青春. All rights reserved.
//

#import "XSHeaderView.h"
#import "XSCollectionCell.h"
@implementation XSHeaderView


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatCollectionviewMethod];
        [self showXSButtonImageDataMethodWithUrl:[MethodClass returnUrlSetStrWSDLURL:@"/dashop/seckill/app/getSecKill.do"]];
        
    }
    return self;
}


//限时抢购按钮图
-(void)showXSButtonImageDataMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        self.dataArray = response;
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            [self.listCollectionview reloadData];
        }];
    } failBlock:^(NSError *error) {
        
    }];
    
}


-(void)creatCollectionviewMethod{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.listCollectionview = [[ UICollectionView alloc ] initWithFrame:CGRectMake(0, 1, kScreenWidth, 50*kJLXWidthScale) collectionViewLayout :layout];
    self.listCollectionview.showsHorizontalScrollIndicator = NO;
    self.listCollectionview.alwaysBounceHorizontal = NO;
    [self addSubview:self.listCollectionview];
    if (kScreenHeight == 812) {
        self.listCollectionview.sd_layout
        .bottomSpaceToView(self, 34);
    }
    
    self.listCollectionview.delegate = self;
    self.listCollectionview.dataSource = self;
    self.listCollectionview.backgroundColor = [UIColor whiteColor];
    [self.listCollectionview registerClass :[ XSCollectionCell class ] forCellWithReuseIdentifier : @"onecell" ];
}


#pragma mark--------------------UICollectionViewDelegateFlowLayout,UICollectionViewDelegate, UICollectionViewDataSource
//cell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
//
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    XSCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"onecell" forIndexPath:indexPath];
    NSDictionary *dic = self.dataArray[indexPath.row];
    cell.titlab.text = [NSString stringWithFormat:@"%@",dic[@"SECKILL_NAME"]];
    cell.titlab.textColor = [UIColor orangeColor];
    return cell;
}


#pragma mark-------------------布局代理
//设置每个cell的宽和高
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dataArray.count < 5) {
         return CGSizeMake(kScreenWidth/self.dataArray.count, 50*kJLXWidthScale);
    }else{
        return CGSizeMake(150*kJLXWidthScale, 50*kJLXWidthScale);
    }
}
//设置分区缩进
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
//设置每个分区之间最小的行间距和列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}
#pragma mark--------------------------cell点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    XSCollectionCell * cell = (XSCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
    NSDictionary *dic = self.dataArray[indexPath.row];
    self.headerBlock(dic,indexPath.row);
    cell.titlab.textColor = [UIColor redColor];
    
    
}

-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    XSCollectionCell * cell = (XSCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.titlab.textColor = [UIColor orangeColor];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
